#define LOG_TAG "[FaceSaverPlugin]"
#define _DEBUG_OUTPUT_ true

#include <opencv2/opencv.hpp>
#include "../BaseData.h"
#include "../GetFace.h"

using namespace std;
using namespace cv;

class FaceSaverPlugin : public FaceMatProcessor {
    public:
        virtual int process(cv::Mat &, std::vector<std::string> &args);
        virtual int processOriginal(cv::Mat &orig, std::vector<std::string> &args);
    private:
        static int g_process_count;
};
int FaceSaverPlugin::g_process_count = 0;

extern "C" FaceMatProcessor *plugin_create(void ) {
    return new FaceSaverPlugin();
}

extern "C" void plugin_destroy(FaceMatProcessor *p) {
    if(NULL != p) {
        delete p;
    }
}

int FaceSaverPlugin::process(cv::Mat &faceImg, std::vector<std::string> &args) {
    if(args.size() == 0) {
        cdebug("args too few, need indicate [OUTPUT_DIR]\n");
        return -1;
    }
    
    stringstream ss;
    ss << args[0] << "/FaceSaverPlugin_" << g_process_count << ".bmp";
    
    Mat resizedImg(30, 30, faceImg.type());
    Mat resizedGray(30, 30, CV_8UC1);
    resize(faceImg, resizedImg, Size(30, 30));
    if(faceImg.channels() >= 3) {
        cvtColor(resizedImg, resizedGray, CV_BGR2GRAY);
    } else {
        resizedGray = resizedImg;
    }

    cv::Size sz = resizedGray.size();
    /*
    cdebug("resizedGray size=%dx%d, channels=%d, isContinuous=%s\n", 
            sz.width, sz.height, resizedGray.channels(), 
            resizedGray.isContinuous() ? "true" : "false");
    */
 
    imwrite(ss.str(), resizedGray);
    cdebug("face image saved to %s\n\n", ss.str().c_str());
    g_process_count ++;

    return 0;
}

int FaceSaverPlugin::processOriginal(cv::Mat &orig, std::vector<std::string> &args) {
    //not need to do anything currently
    return 0;
}
